package itheima_edu_info_manager.com.itheima.edu.info.manager.dao;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    //将数组变为成员变量，并用static进行修饰（将此数组变成公共数组）
    private static Teacher[] teachers = new Teacher[5];

    //添加老师的操作
    public boolean addTeacher(Teacher teacher) {
        //假设数组已经满了
        int index = -1;
        //遍历数组
        for (int i = 0; i < teachers.length; i++) {
            Teacher te = teachers[i];
            //判断数组是否存在null
            if (te == null){
                index = i;
                //****只要找到null，需要将循环结束，否则循环继续程序可能会出现问题****
                break;
            }
        }
        //判断数组是否为满
        if (index == -1){
            //数组已经满了
            System.out.println("添加失败");
            return false;
        }else {
            //数组没有满，将封装好的数据传入其中
            teachers[index] = teacher;
            return true;
        }
    }

    //查询老师的操作
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    //删除老师的操作
    public void deleteTeacherById(String id) {
        //调用getIndex()方法，将接收的id传入其中，收到对应的数组索引位置
        int index = getIndex(id);
        //直接将对应的数组信息，用null进行替换
        teachers[index] = null;
    }

    //获取输入的id，所在数组的索引位置
    public int getIndex(String id) {
        //假设没有对应的索引位置
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher te = teachers[i];
            if (te != null && te.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    //修改老师的操作
    public void upDateTeacherById(String id, Teacher teacher) {
        int index = getIndex(id);
        teachers[index] = teacher;
    }
}
